package com.bra.lunbo;

import java.util.ArrayList;

import com.bra.R;
import com.bra.ui.activity.CarouselDetailsActivity;
import com.bra.view.ImagerLoaderHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

@SuppressLint("NewApi")
public class CarouselPageAdapter extends PagerAdapter {

	private ArrayList<MainPage> bannerList;
	private Context mContext;
	private ImageLoader imageLoader;
	private ImagerLoaderHelper imageLoaderHelper;

	public CarouselPageAdapter(Context context, ArrayList<MainPage> bannerList) {
		this.mContext = context;
		this.bannerList = bannerList;
		imageLoader = ImageLoader.getInstance();
		imageLoaderHelper = ImagerLoaderHelper.getInstance();

	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		if (bannerList.size() == 0) {
			return null;
		}

		View view = LayoutInflater.from(mContext).inflate(R.layout.galary_item,
				null);
		final ImageView iv = (ImageView) view.findViewById(R.id.iv_galary);
		final MainPage ban = bannerList.get(position);
		if (ban != null) {				
		     imageLoader.displayImage(ban.slide_url, iv,imageLoaderHelper.getOptions());
		      //轮播图点击事件
		     iv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent= new Intent();
					intent.setClass(mContext, CarouselDetailsActivity.class);					
					intent.putExtra("url", ban.slide_content);
					mContext.startActivity(intent);
					
				}
			});
		}
		container.addView(view);
		return view;
	}

	@Override
	public int getCount() {
		return bannerList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	public void setData() {

	}
}
